/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ca.jerg.canvas;

import javax.swing.undo.*;

import ca.jerg.data.ErgData;
import ca.jerg.data.ErgPoint;
import java.util.Iterator;

/**
 *
 * @author dave
 */
public class EditAdd extends AbstractUndoableEdit
{
	public EditAdd( Canvas canvas, ErgData dataSet, ErgPoint dataPoint )
	{
		this.canvas = canvas;
		data = dataSet;
		point = dataPoint;
		newData = null;
	}
	
	public EditAdd( Canvas canvas, ErgData dataSet, ErgData newData )
	{
		this.canvas = canvas;
		data = dataSet;
		this.newData = newData;
		point = null;
	}
	
	@Override
	public void undo() throws CannotUndoException
	{
		if ( (point != null) && (newData == null) )
			data.remove(point.getTime());
		else if ( (point == null) && (newData != null) )
		{
			Iterator i = newData.getData().iterator();
			while ( i.hasNext() )
			{
				ErgPoint ep = (ErgPoint)i.next();
				data.remove(ep.getTime());
			}
		}
		
		canvas.repaint();
	}
	
	@Override
	public void redo() throws CannotRedoException
	{
		if ( (point != null) && (newData == null) )
			data.insertAt(point.getTime(), point);
		else if ( (point == null) && (newData != null) )
		{
			Iterator i = newData.getData().iterator();
			while ( i.hasNext() )
			{
				ErgPoint ep = (ErgPoint)i.next();
				data.insertAt(ep.getTime(),ep);
			}	
		}
		
		canvas.repaint();
	}
	
	@Override
	public boolean canUndo() 
	{
		return true;
	}

	@Override
	public boolean canRedo() 
	{
		return true;
	}

	@Override
	public String getPresentationName() 
	{
		return "Add";
	}
	
	private Canvas canvas;
	private ErgData data;
	private ErgData newData;
	private ErgPoint point;
}
